package com.huichaoliu.stylist.activity;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import com.huichaoliu.adapters.FragmentShoppingAdapter;
import com.huichaoliu.adapters.FragmentShowPicLeftAdapter;
import com.huichaoliu.adapters.FragmentShowPicRightAdapter;
import com.huichaoliu.app.R;
import com.huichaoliu.data.Config;
import com.huichaoliu.data.UtilInfo;
import com.rederxu.request.HTTPRequest;
import com.rederxu.request.onRequestOverListener;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public abstract class StylistShowPicFragment extends Fragment implements
		OnClickListener {
	//api
	private final static String BASE_URL = "http://211.149.205.68:8080/huichaoliu/";
	public final static String ZONELIST = BASE_URL + "service/action/zonelist.action";
	// tools
	Context context;

	// Views
	private View v = null;
	TextView title_left;
	TextView title_right;

	RelativeLayout relat_send;
	TextView btn_send;

	ListView list_left;
	ListView list_right;
	// head view
	View headView;
	ImageView headPic;
	TextView headName;
	TextView headInfo;
	LinearLayout headMsg;
	ImageView headMsgPic;
	TextView headMsgCount;

	// values
	List<UtilInfo> listLeftInfo;
	List<UtilInfo> listRightInfo;

	// adapter
	FragmentShowPicLeftAdapter leftAdapter;
	FragmentShowPicRightAdapter rightAdapter;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		v = inflater.inflate(R.layout.fragment_stylist_show_pic, container,
				false);
		context = getActivity();
		init();
		getLeftData();
		getRightData();
		return v;
	}

	private void init() {
		title_left = (TextView) v.findViewById(R.id.show_pic_left);
		title_right = (TextView) v.findViewById(R.id.show_pic_right);

		relat_send = (RelativeLayout) v.findViewById(R.id.show_pic_bg);
		btn_send = (TextView) v.findViewById(R.id.show_pic_sendbtn);

		list_left = (ListView) v.findViewById(R.id.show_pic_list_left);
		list_right = (ListView) v.findViewById(R.id.show_pic_list_right);

		changePosition(0);

		title_left.setOnClickListener(this);
		title_right.setOnClickListener(this);
		btn_send.setOnClickListener(this);

		initRightHead();
	}

	@SuppressLint("InflateParams")
	private void initRightHead() {
		headView = LayoutInflater.from(context).inflate(
				R.layout.showpic_header, null);
		headInfo = (TextView) headView.findViewById(R.id.showpic_head_info);
		headName = (TextView) headView.findViewById(R.id.showpic_head_username);
		headPic = (ImageView) headView.findViewById(R.id.showpic_head_pic);
		headMsg = (LinearLayout) headView.findViewById(R.id.showpic_msg_bg);
		headMsgCount = (TextView) headView.findViewById(R.id.showpic_msg_count);
		headMsgPic = (ImageView) headView.findViewById(R.id.showpic_msg_pic);

		headMsg.setVisibility(View.VISIBLE);
		headMsg.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(context,
						StylistNewNotifyActivity.class);
				startActivity(intent);
			}
		});
		headPic.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
			}
		});

		if (list_right.getHandler() == null) {
			list_right.addHeaderView(headView);
		}

	}

	private void getLeftData() {
		listLeftInfo = new ArrayList<UtilInfo>();
//		for (int i = 0; i < 20; i++) {
//			listLeftInfo.add(new UtilInfo());
//		}
		// if (leftAdapter == null) {
//		leftAdapter = new FragmentShowPicLeftAdapter(listLeftInfo, context);
//		list_left.setAdapter(leftAdapter);
		// } else {
		// leftAdapter.notifyDataSetChanged();
		// }
		Bundle bundle = new Bundle();
		bundle.putString("userId", "51b634b6361a07f1");
		bundle.putString("userId", "51b634b6361a07f1");
		bundle.putString("pagesize", "10");
		bundle.putString("pageno", "0");
		HTTPRequest.POST(getActivity(), ZONELIST,
				bundle, new onRequestOverListener() {

					@Override
					public void onRequestOver(String result) {
						Log.i("Hello", "bundle = " + result);
						try {
							JSONObject jo = new JSONObject(result);
							if (jo.getInt("code") != 0) {
								Toast.makeText(context,
										jo.getString("message"), 1).show();
								return;
							}
							Message msg = new Message();
							msg.what = 1;
							msg.obj = result;
							mHandler.sendMessage(msg);

						} catch (JSONException e) {
							if (result.contains("500")) {
								try {
									JSONObject jo = new JSONObject(result);
									Toast.makeText(getActivity(),
											jo.getString("message"), 1).show();
								} catch (JSONException e2) {

								}
							}
							e.printStackTrace();
						}
					}
				});
	}

	@SuppressWarnings("deprecation")
	private void getRightData() {
		listRightInfo = new ArrayList<UtilInfo>();
		for (int i = 0; i < 20; i++) {
			UtilInfo info = new UtilInfo();
			info.setCommentCount("" + i);
			info.setDiamonCount(i + 1 + "");
			listRightInfo.add(info);
		}
		rightAdapter = new FragmentShowPicRightAdapter(listRightInfo, context);
		list_right.setAdapter(rightAdapter);

		// headPic.setBackgroundDrawable(context.getResources().getDrawable(
		// R.drawable.welcome));
		headName.setText("小仙女|时尚搭配师");
		headInfo.setText("四川成都  | 双子 | 画画 | 特级搭配师 | 搭配数量:900");
		headMsgCount.setText("2条新消息");
		headMsgPic.setBackgroundDrawable(context.getResources().getDrawable(
				R.drawable.ic_avatar));
	}

	@SuppressWarnings("deprecation")
	private void changePosition(int code) {
		switch (code) {
		case 0:
			title_left.setBackgroundDrawable(getResources().getDrawable(
					R.drawable.stylist_showpic_n));
			title_right.setBackgroundDrawable(getResources().getDrawable(
					R.drawable.stylist_showpic_p));
			relat_send.setVisibility(View.VISIBLE);
			list_left.setVisibility(View.VISIBLE);
			list_right.setVisibility(View.GONE);
			showOrHide(false);
			break;
		case 1:
			title_left.setBackgroundDrawable(getResources().getDrawable(
					R.drawable.stylist_showpic_p));
			title_right.setBackgroundDrawable(getResources().getDrawable(
					R.drawable.stylist_showpic_n));
			relat_send.setVisibility(View.GONE);
			list_left.setVisibility(View.GONE);
			list_right.setVisibility(View.VISIBLE);
			showOrHide(true);
			break;

		default:
			break;
		}
	}

	public boolean isShowAdd() {
		if (relat_send == null) {
			return false;
		}
		if (relat_send.getVisibility() == View.VISIBLE) {
			return false;
		}
		return true;
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.show_pic_left:
			changePosition(0);
			break;
		case R.id.show_pic_right:
			changePosition(1);
			// if (rightAdapter == null) {
			// getRightData();
			// }
			break;
		case R.id.show_pic_sendbtn:
			Intent intent = new Intent(context, StylistNewPhotoActivity.class);
			startActivity(intent);
			break;
		case R.id.stylist_show_pic_add:
			Intent intentAdd = new Intent(context,
					StylistLifeTimeActivity.class);
			startActivity(intentAdd);
			break;

		default:
			break;
		}
	}
	
	Handler mHandler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				Log.i("Hello", "1");
				parseLeftData((String) msg.obj, listLeftInfo);
				leftAdapter = new FragmentShowPicLeftAdapter(listLeftInfo, context);
				list_left.setAdapter(leftAdapter);
				break;

			default:
				break;
			}
		}
	};
	
	private void parseLeftData(String res,List<UtilInfo> list) {
		
	}

	public abstract void showOrHide(boolean bool);
}
